import { UserProfilesAPI } from "@/request/api";
export default {
  namespaced: true,
  // 数据
  state: {
    //购物车数量
    cartTotal: 0,
    //用户信息
    userInfo: {
      //昵称
      nickName: "昵称",
      //积分
      coin: 1000,
      //用户头像
      headImg: require("../../assets/img/service.848ec511.png"),
    },
  },
  mutations: {
    //修改用户信息方法
    updateUserInfo(state, payload) {
      state.cartTotal = payload.cartTotal;
      state.userInfo = payload.userInfo;
    },
    //初始化用户信息
    initUserInfo(state) {
      state.cartTotal = 0;
      state.userInfo = {
        //昵称
        nickName: "昵称",
        //积分
        coin: 1000,
        //用户头像
        headImg: require("../../assets/img/service.848ec511.png"),
      };
    },
  },
  actions: {
    //获取用户信息(异步) + 更新vuex中用户数据(触发mutaions)
    async asyncChangeUserInfo({ commit }, payload) {
      //获取用户信息(异步)
      let res = await UserProfilesAPI();
      console.log("用户信息为", res);
      //更新vuex中用户数据(触发mutaions)
      commit("updateUserInfo", res.data);
    },
  },
};
